using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utils;
using System.Data;
using System.Data.SQLite;
using Models.ServerModels;

namespace DdnsServer.DAL
{
    public class UserService
    {
        #region 转换模型
        private LocalUserModel DrToUser(DataRow dr)
        {
            LocalUserModel lm = new LocalUserModel
            {
                id=dr.IsNull("id")?0:Convert.ToInt32(dr["id"]),
                user_id=dr.IsNull("user_id")?"":dr["user_id"].ToString(),
                user_name=dr.IsNull("user_name")?"":dr["user_name"].ToString()
            };
            return lm;
        }
        
        #endregion
        #region 读用户信息
        public LocalUserModel GetUserInfo(string user_id)
        {
            string sqlstr = "select * from t_user where user_id=@user_id";
            SQLiteParameter[] op = new SQLiteParameter[]
            {
                new SQLiteParameter {DbType=DbType.String,ParameterName="@user_id",Value=user_id}
            };
            var dt = DB.Query(sqlstr, op);
            if (dt.Rows.Count < 1)
                return null;
            return DrToUser(dt.Rows[0]);
        }
        #endregion
   
        #region 保存日志
        public void SaveLog(int userId,string domain,string remark)
        {
            try
            {
                string sqlstr = "insert into t_log(logdate,user_id,domain,remark) values(@logdate,@user_id,@domain,@remark)";
                SQLiteParameter[] op = new SQLiteParameter[]
                {
                    new SQLiteParameter {DbType=DbType.DateTime,ParameterName="@logdate",Value=DateTime.Now },
                    new SQLiteParameter {DbType=DbType.Int32,ParameterName="@user_id",Value=userId },
                    new SQLiteParameter {DbType=DbType.String,ParameterName="@domain",Value=domain },
                    new SQLiteParameter {DbType=DbType.String,ParameterName="@remark",Value=remark}
                };
                DB.Exec(sqlstr, op);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}